package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Vector;

import com.db.DBConn;
import model.Admin;



public class AdminDao {

    public Collection getDatas() throws SQLException {
        DBConn db = new DBConn();
        ResultSet rs = db.queryData("select * from admin order by id");
        Collection col =  new Vector();
        while(rs.next()) {
            Admin admin = new Admin();
            admin.setId(rs.getString("id"));
            admin.setAno(rs.getString("ano"));
            admin.setAname(rs.getString("aname"));
            admin.setPassword(rs.getString("password"));
            col.add(admin);
        }
        return col;
    }

    //提取管理员信息by id
    public Admin getData(String id) throws SQLException {
        DBConn db = new DBConn();
        ResultSet rs = db.queryData("select * from admin where id="+id);
        if(rs.next()) {
            Admin admin = new Admin();
            admin.setId(id);
            admin.setAno(rs.getString("ano"));
            admin.setAname(rs.getString("aname"));
            admin.setPassword(rs.getString("password"));
            return admin;
        }else {
            return null;
        }
    }

    //修改数据
    public void updateData(Admin admin) throws SQLException {
        DBConn db = new DBConn();
        //String ano = admin.getAno();
        db.updateData("update admin set ano='"+admin.getAno()+"',aname='"+admin.getAname()+"',password='"+admin.getPassword()+"' where id="+admin.getId());
    }

    //新增数据
    public void insertData(Admin admin) throws SQLException {
        DBConn db = new DBConn();
        db.updateData("insert into admin(ano,aname,password) values('"+admin.getAno()+"','"+admin.getAname()+"','"+admin.getPassword()+"')");
    }

    //删除数据
    public void deleData(String id) throws SQLException {
        DBConn db = new DBConn();
        db.updateData("delete from admin where id="+id);

    }
}


